package controllers;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.Map;

import model.packModel;
import view.PackageReportGUI;
import client.CliMessage;

public class PackageReportController extends AbstractController implements IValidateInput
{	
	private PackageReportGUI PackageReportView;
	private packModel packModel;

	
	public PackageReportController( PackageReportGUI PackageReportView, packModel packModel) 
	{
		super(PackageReportView,packModel);
		this.PackageReportView = PackageReportView;
		this.packModel = packModel;
	}
	
	
	public void setButtons(packModel packModel)
	{		
		PackageReportView.addReplyActionListener(new ReplyListener(packModel));
	}
		

	@Override
	public boolean validateInput() 
	{
		return true;		
	}
	
	

	/**
	 * Inner class that handles when Reply Button Pressed, implements Action listener
	 * @author Tal & Yuda
	 *
	 */
	
	class ReplyListener implements ActionListener
	{
		
		private packModel packModel;
		
		public ReplyListener(packModel packModel)
		{
			this.packModel = packModel;
		}
		
		@Override
		public void actionPerformed(ActionEvent arg0) 
		{
			if(!validateInput())
				return;
           String packInfo = packModel.getDetails();
           int newPack = packModel.getAmount();
           double used = packModel.getUsage();
			String insertQuery = "adu"	+"INSERT INTO package_report "
										+"(details, newPack, packUsage) VALUES "
										+"('"+ packInfo +"', "+ newPack +", "+ used +")";

			
             //activate the back button
		  	   PackageReportView.clickBackButton();
               sendToServer(insertQuery); 
		}
	}

	@Override
	public void handleDBRequestSuccess()
	{
		PackageReportView.displayWrnMessage("Reply Succesfully Enterd");		
	}

	
	
	@Override
	public void handleDBSearchResult(CliMessage dbResult)
	{	
		ArrayList<HashMap<String, String>> srs = dbResult.getSrs();
		String packInfo ="";
		Calendar cal = Calendar.getInstance();
		int newPack = 0;
		int usedPack = 0;
		double Usage = 0;
		
		int currMonth = cal.get(Calendar.MONTH);
		currMonth++;
		int currYear = cal.get(Calendar.YEAR);
		


		int complaintNum=srs.size();
		for(int i = 0 ; i < complaintNum  ; i++)
		{
		   Map<String, String> complaintTupple  = srs.get(i);	
  
		   int DBmonth = (Integer.parseInt((complaintTupple.get("TIMESTAMP")).substring(5, 7)));
		   int DByear = (Integer.parseInt((complaintTupple.get("TIMESTAMP")).substring(0, 4)));
		   int DBday = (Integer.parseInt((complaintTupple.get("TIMESTAMP")).substring(8, 10)));
		   
		   System.out.println ("year: " + DByear + " month: " + DBmonth + " day: " + DBday);
					
		   if ((currMonth == DBmonth) && (currYear == DByear) && ( DBday >= 1) && ( DBday <=31))
		   {
			   packInfo = packInfo + "User ID: " +(complaintTupple.get("UID")) + "Package used: " + (100 - (Double.parseDouble(complaintTupple.get("BALANCE"))) / 2) + "%\n";
			   usedPack = usedPack + 100 - (Integer.parseInt(complaintTupple.get("BALANCE")));
			   newPack++;
		   }
		}	
		
		Usage = (double) (100 - ((double)(newPack / newPack) / 2));
		
		packModel.setDetails(packInfo);
		packModel.setUsage(Usage);
		packModel.setAmount(newPack);
		PackageReportView.setPackReport (packInfo);
		///set fields GUi
		setButtons(packModel);
		//display main window
		PackageReportView.setVisible(true);						
	}

	@Override
	public void handleNullSearchResult() 
	{
		PackageReportView.displayWrnMessage("DATA DOES NOT EXIST!");
		
	}

}
